package com.ztjava.weixin.controller;

import java.util.Date;

import com.jfinal.aop.Duang;
import com.jfinal.kit.Ret;
import com.xiaoleilu.hutool.date.DateField;
import com.xiaoleilu.hutool.date.DateUtil;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;
import com.ztjava.weixin.model.scene.SceneForever;
import com.ztjava.weixin.service.SceneService;

@ControllerClass(client=PortalClient.class,path="/weixin/scene")
@UserController(portal=Constant.P_PORTAL)
public class ScenePortalController extends BaseController{
	/*************
	 *SceneAdminController.java
	 *@ztjava.com
	 *2018-07-09 13:54:01
	**************/
	private static SceneService sceneService = Duang.duang(SceneService.class);
	/*************
	 *修改Forever
	**************/
	@UserMethod("WeixinSceneForever")
	public void addEditForever(){
		Integer id = getParaToInt("id",0);
		SceneForever sceneForever = new SceneForever();
		if(id>0){
			sceneForever = SceneForever.dao.findById(id);
		}else {
			sceneForever.set("plugin", "WEIXIN");
			sceneForever.set("component", "SCENE");
			sceneForever.set("model", "FOREVER");
		}
		setAttr("sceneForever",sceneForever);
		render("addEditForever.html");
	}

	/*************
	 *保存Forever
	**************/
	@UserMethod("WeixinSceneForever")
	public void saveForever(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		SceneForever sceneForever = getModel(SceneForever.class);
		saveModel(sceneForever,authSession.getId());
		if(sceneForever.get("url")==null||sceneForever.getStr("url").equals("")){
			Ret ret = sceneService.updateForeverUrl(sceneForever);
			if(ret.getStr(Constant.RESULT).equals(Constant.ERROR)){
				renderSuccess("信息保存成功但二维码生成失败！");
				return;
			}
		}
		renderSuccess();
	}

	/*************
	 *Forever列表
	**************/
	@UserMethod("WeixinSceneForever")
	public void foreverList(){
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("foreverList.html");
			return;
		}
		renderPageJson(sceneService.getForeverPage(client, getParaMapCustom(new String[]{"plugin","WEIXIN"}
		,new String[]{"component","SCENE"}
		,new String[]{"model","FOREVER"})));
	}
	

	/*************
	 *Record列表
	**************/
	@UserMethod("WeixinSceneRecord")
	public void recordList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			setAttr("start_time", DateUtil.offset(new Date(), DateField.MONTH, -1).toString("yyyy-MM-dd 00:00:00"));
			setAttr("end_time", DateUtil.format(new Date(), "yyyy-MM-dd 23:59:59"));
			keepPara();
			render("recordList.html");
			return;
		}
	
		
		renderPageJson(sceneService.getRecordPage(client, getParaMap()));
	}
}
